<?php
/**
 * Created by PhpStorm.
 * User: geolo
 * Date: 2018/9/25
 * Time: 16:53
 */
defined('YII_RUN') or exit('Access Denied');
$urlManager = \Yii::$app->urlManager;
$this->title = '内容页';
$this->params['active_nav_group'] = 1;

?>
<div class="panel mb-3">
    <div class="panel-header"><?= $this->title ?>
        <button onclick="back()">返回</button>
    </div>
    <div class="panel-body">
        <button style="float:right"  onclick="choose()">选择商品</button>
        <form class="auto-form" id="auto-form" method="post" >

            <input type="hidden" name="id" value="<?=$res->id?>">
            <div class="form-group row">
                <div class="form-group-label col-sm-2 text-right">
                    <label class="col-form-label required">主标题</label>
                </div>
                <div class="col-sm-6">
                    <input class="form-control cat-name" name="title" id="title" value="<?=$data['title']?>">
                </div>
            </div>
            <div class="form-group row">
                <div class="form-group-label col-sm-2 text-right">
                    <label class="col-form-label required">副标题</label>
                </div>
                <div class="col-sm-6">
                    <input class="form-control cat-name" name="title_s" id="title_s"
                           value="<?= $data['title_s']?>">
                </div>
            </div>
            <div class="form-group row">
                <div class="form-group-label col-sm-2 text-right">
                    <label class="col-form-label required">首页图片</label>
                </div>
                <div class="col-sm-9">

                    <div class="col-9">
                        <div class="upload-group short-row">
                            <div class="input-group" style="width:79%">
                                <input class="form-control file-input" name="image" id="image"
                                       value="<?= $data['image']?>">
                                <span class="input-group-btn">
                                        <a class="btn btn-secondary upload-file" href="javascript:"
                                           data-toggle="tooltip"
                                           data-placement="bottom" title="上传文件">
                                            <span class="iconfont icon-cloudupload"></span>
                                        </a>
                                    </span>
                                <span class="input-group-btn">
                                        <a class="btn btn-secondary select-file" href="javascript:"
                                           data-toggle="tooltip"
                                           data-placement="bottom" title="从文件库选择">
                                            <span class="iconfont icon-viewmodule"></span>
                                        </a>
                                    </span>
                                <span class="input-group-btn">
                                        <a class="btn btn-secondary delete-file" href="javascript:"
                                           data-toggle="tooltip"
                                           data-placement="bottom" title="删除文件">
                                            <span class="iconfont icon-close"></span>
                                        </a>
                                    </span>
                            </div>
                            <div class="upload-preview text-center upload-preview" >
                                <span class="upload-preview-tip">690 &times; 390</span>
                                <img class="upload-preview-img" id='imgshow'src="<?=$data['image']?>">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="form-group row">
                <div class="form-group-label col-sm-2 text-right">
                    <label class="col-form-label">选择专题</label>
                </div>
                <div class="col-sm-6">
                    <select name="subject_id" id="subject_id">
                        <?php foreach($subject as $key=>$sub):?>
                        <option value="<?= $sub['id']?>"<?php if($sub['id']==$data['subject_id']){?>selected<?php }?>><?= $sub['title']?></option>
                        <?php endforeach;?>
                    </select>
                </div>
            </div>
            <div class="form-group row">
                <div class="form-group-label col-sm-2 text-right">
                    <label class="col-form-label">是否显示在首页</label>
                </div>
                <div class="col-sm-6">
                    <label class="radio-label">
                        <input id="radio2" <?= $data['type'] == 1 ? 'checked' : null ?>
                               value="1"
                               name="type" type="radio" class="custom-control-input">
                        <span class="label-icon"></span>
                        <span class="label-text">是</span>
                    </label>
                    <label class="radio-label">
                        <input id="radio1" <?= $data['type'] == 0 ? 'checked' : null ?>
                               value="0"
                               name="type" type="radio" class="custom-control-input">
                        <span class="label-icon"></span>
                        <span class="label-text">否</span>
                    </label>
                </div>
            </div>
            <div class="form-group row">
                <div class="form-group-label col-sm-2 text-right">
                    <label class="col-form-label">视频来源</label>
                </div>
                <div class="col-sm-6">
                    <label class="radio-label">
                        <input id="radio2" <?= $data['video_type'] == 0 ? 'checked' : null ?>
                               value="0"
                               name="video_type" type="radio" class="custom-control-input">
                        <span class="label-icon"></span>
                        <span class="label-text">源地址</span>
                    </label>
                    <label class="radio-label">
                        <input id="radio1" <?= $data['video_type'] == 1 ? 'checked' : null ?>
                               value="1"
                               name="video_type" type="radio" class="custom-control-input">
                        <span class="label-icon"></span>
                        <span class="label-text">腾讯</span>
                    </label>
                </div>
            </div>
            <div class="form-group row">
                <div class="form-group-label col-sm-2 text-right">
                    <label class="col-form-label">视频链接</label>
                </div>
                <div class="col-sm-6">
                    <div class="video-picker" data-url="<?= $urlManager->createUrl(['upload/video']) ?>">
                        <div class="input-group">
                            <input class="video-picker-input video form-control" name="video" id="video"
                                   value="<?= $data['video'] ?>" placeholder="请输入视频源地址或者选择上传视频">
                            <a href="javascript:" class="btn btn-secondary video-picker-btn">选择视频</a>
                        </div>
                        <a class="video-check" id="video-check" href="<?= $data['video'] ? $data['video'] : "javascript:" ?>"
                           target="_blank">视频预览</a>

                        <div class="video-preview"></div>
                        <div><span
                                    class="text-danger">支持格式mp4;支持编码H.264;视频大小不能超过<?= \app\models\UploadForm::getMaxUploadSize() ?>
                                M</span></div>
                    </div>
                </div>
            </div>
            <div class="form-group row">
                <div class="form-group-label col-sm-2 text-right">
                    <label class="col-form-label">内容</label>
                </div>
                <div class="col-sm-6">
                        <textarea id="editor" style="width: 100%"
                                  name="content"><?= $data['content'] ?></textarea>
                </div>
            </div>
<!--            <div class="form-group row">-->
<!--                <div class="form-group-label col-sm-2 text-right">-->
<!--                    <label class="col-form-label">内容</label>-->
<!--                </div>-->
<!--                <div class="col-sm-6">-->
<!--                        <textarea id="editor" style="width:800px;height:400px;visibility:hidden;display: block;"-->
<!--                                  name="content">--><?//= $data['content'] ?><!--</textarea>-->
<!--                </div>-->
<!--            </div>-->

            <div class="form-group row">
                <div class="form-group-label col-sm-2 text-right">
                </div>
                <div class="col-sm-6">
                    <a class="btn btn-primary auto-form-btn" href="javascript:">保存</a>
                </div>
            </div>
        </form>
    </div>
</div>
<?php //echo Yii::$app->request->baseUrl;?>
<script src="<?= Yii::$app->request->baseUrl ?>/statics/ueditor/ueditor.config.js"></script>
<script src="<?= Yii::$app->request->baseUrl ?>/statics/ueditor/ueditor.all.min.js"></script>
<script src="<?= Yii::$app->request->baseUrl ?>/statics/kindeditor/kindeditor-all.js"></script>
<script src="<?= Yii::$app->request->baseUrl ?>/statics/kindeditor/kindeditor-all-min.js"></script>
<script src="<?= Yii::$app->request->baseUrl ?>/statics/kindeditor/lang/zh-CN.js"></script>
<script>
    var ue = UE.getEditor('editor', {
        serverUrl: "<?=$urlManager->createUrl(['upload/ue'])?>",
    });
</script>
<script>
    function choose(){
         var id='<?=$_GET['id']?>';
         console.log(id);
        if(id=='') {
            if (confirm('请先编辑内容并保存之后再选择商品')) {
                var newurl = '<?= $urlManager->createUrl(['mch/content/good', 'content_id' => $data['id']])?>';
                window.location.href = newurl;
            }
            return false;
        }else{
            var newurl = '<?= $urlManager->createUrl(['mch/content/good', 'content_id' => $data['id']])?>';
            window.location.href = newurl;
        }
    }
    function back(){
        var newurl='<?= $urlManager->createUrl(['mch/content/content-list'])?>';
        window.location.href=newurl;
    }
//    var editor;
//    KindEditor.ready(function(K) {
//        editor = K.create('textarea[name="content"]', {
//            cssPath : '<?//= Yii::$app->request->baseUrl ?>///statics/kindeditor/plugins/code/prettify.css>',
//            uploadJson : '<?//= Yii::$app->request->baseUrl ?>///statics/kindeditor/php/upload_json.php',
//            fileManagerJson : '<?//= Yii::$app->request->baseUrl ?>///statics/kindeditor/php/file_manager_json.php',
//            allowFileManager : true,
//            afterCreate : function() {
//                var self = this;
//                K.ctrl(document, 13, function() {
//                    self.sync();
//                    K('form[name=example]')[0].submit();
//                });
//                K.ctrl(self.edit.doc, 13, function() {
//                    self.sync();
//                    K('form[name=example]')[0].submit();
//                });
//            }
//        });
//        prettyPrint();
//        editor.sync();
//    });
$(document).ready(function () {
    var video_picker = $('.video-picker');
    video_picker.each(function (i) {
        var picker = this;
        var el = $(this);
        var btn = el.find('.video-picker-btn');
        var url = el.data('url');
        var input = el.find('.video-picker-input');
        var view = el.find('.video-preview');

        function uploaderVideo() {

            var el_id = $.randomString(32);
            btn.attr("id", el_id);

            var uploader = new plupload.Uploader({
                runtimes: 'html5,flash,silverlight,html4',
                browse_button: el_id, // you can pass an id...
                url: url,
                flash_swf_url: '<?=$statics?>/mch/js/Moxie.swf',
                silverlight_xap_url: '<?=$statics?>/mch/js/Moxie.xap',

                filters: {
                    max_file_size: '100mb',
                    mime_types: [
                        {title: "Video files", extensions: "mp4"}
                    ]
                },

                init: {
                    PostInit: function () {

                    },

                    FilesAdded: function (up, files) {
                        $('.form-error').hide();
                        uploader.start();
                        btn.btnLoading("正在上传");
                        uploader.disableBrowse(true);

                        plupload.each(files, function (file) {
                            console.log(file)
                            view.html('<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div>');
                        });
                    },
                    FileUploaded: function (uploader, file, responseObject) {
                        console.log(responseObject.status);
                        if (responseObject.status == undefined || responseObject.status != 200) {
                            return true;
                        }
                        var res = $.parseJSON(responseObject.response);
                        if (res.code != 0) {
                            $('.form-error').html(res.msg).show();
                            return true;
                        }
                        $(input).val(res.data.url);
                        $('.video-check').prop('href', res.data.url);
                        $('.video-preview').find('span').html('100%');
                    },

                    UploadProgress: function (up, file) {
                        var percent = file.percent - 1;
                        $($("#" + file.id).find('b')[0]).html('<span>' + percent + "%</span>");
//                            document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
                    },

                    Error: function (up, err) {
                        $('.form-error').html('文件大小超出配置').show();
//                            document.getElementById('console').appendChild(document.createTextNode("\nError #" + err.code + ": " + err.message));
                    },
                    UploadComplete: function (uploader, files) {
                        btn.btnReset();
                        uploader.destroy();
                        uploaderVideo();
                    }
                }
            });
            uploader.init();
        }

        uploaderVideo();
    });
});
$(document).on('change', '.video', function () {
    $('.video-check').attr('href', this.value);
});

</script>

